package com.zoffy.oj_backend.controller;

import com.zoffy.oj_backend.common.BaseResponse;
import com.zoffy.oj_backend.common.ResultUtils;
import com.zoffy.oj_backend.model.dto.user.UserLoginRequest;
import com.zoffy.oj_backend.model.dto.user.UserRegistryRequest;
import com.zoffy.oj_backend.model.vo.UserVo;
import com.zoffy.oj_backend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/registry")
    public BaseResponse<Long> userRegistry(@RequestBody UserRegistryRequest userRegistryRequest){
        String userAccount = userRegistryRequest.getUserAccount();
        String userPassword = userRegistryRequest.getUserPassword();
        String checkPassword = userRegistryRequest.getCheckPassword();
        long userId=userService.userRegistry(userAccount,userPassword,checkPassword);
        return ResultUtils.success(userId);
    }
    @PostMapping("/login")
    public BaseResponse<UserVo> userLogin(HttpServletRequest request, @RequestBody UserLoginRequest userLoginRequest){
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();

        UserVo userVo = userService.userLogin(request, userAccount, userPassword);
        return ResultUtils.success(userVo);
    }
    @GetMapping("/current")
    public BaseResponse<UserVo> currentUserVo(HttpServletRequest request){

        UserVo userVo = userService.currentUserVo(request);
        return ResultUtils.success(userVo);
    }
    @PostMapping("/avatar")
    public BaseResponse<Boolean> updateAvatar(HttpServletRequest request, @RequestPart MultipartFile file){
        boolean b = userService.uploadAvatar(request, file);
        return ResultUtils.success(b);
    }
}
